package club.hicode.daydayup.sslFile.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * Simple SSL chat server modified from {@link TelnetServer}.
 */
public class SecureChatServer {

    private final int port;

    private final String sslMode;

    public SecureChatServer(int port, String sslMode) {
		this.port = port;
		this.sslMode = sslMode;
    }

    public void run() throws InterruptedException {
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
		    ServerBootstrap b = new ServerBootstrap();
		    b.group(bossGroup, workerGroup)
			    .channel(NioServerSocketChannel.class)
			    .handler(new LoggingHandler(LogLevel.INFO))
			    .childHandler(new SecureChatServerInitializer(sslMode));
	
		    b.bind(port).sync().channel().closeFuture().sync();
		} finally {
		    bossGroup.shutdownGracefully();
		    workerGroup.shutdownGracefully();
		}
    }

    public static void main(String[] args) throws Exception {
		/*if (args.length != 1) {
		    System.err.println("Please input ssl mode");
		    System.exit(-1);
	
		}*/
		String sslMode = "CA";
		int port = 8443;
		new SecureChatServer(port, sslMode).run();
    }
}
